package com.woniu.utils;

import com.woniu.entity.User1;
import com.woniu.pojo.Power;
import com.woniu.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

//登录拦截器
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //获得Session对象
        HttpSession session = httpServletRequest.getSession();
        //获得登录用户的信息
        User1 loginUser = (User1) session.getAttribute("user");
        if(loginUser==null){
            httpServletResponse.sendRedirect("login.jsp");
            return false;
        }
//        else{
//            //权限查看并判断是否包含没包含,返回false
//            List<Power> tree = (List<Power>) httpServletRequest.getSession().getAttribute("tree");
//           String path="/"+httpServletRequest.getServletPath();
//
//           if(tree!=null&&tree.size()>0){
//               return judge( tree, path);
//           }else{
//               return false;
//           }
//
//
//
//        }
            return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
//    public  boolean judge(List<Power> tree,String path){
//
//        for(Power p:tree){
//            if(p.getPowerUrl()!=null&&path.equals(p.getPowerUrl().substring(0,path.length()))){
//                return true;
//            }else if(p.getChildren()!=null){
//                judge(p.getChildren(),path);
//            }else {
//                return false;
//            }
//        }
//        return false;
//    }

}
